package com.taobao.ic.jingwei;

import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.taobao.tddl.dbsync.applier.Applier;
import com.taobao.tddl.dbsync.applier.ApplierException;
import com.taobao.tddl.dbsync.dbms.DBMSAction;
import com.taobao.tddl.dbsync.dbms.DBMSEvent;
import com.taobao.tddl.dbsync.dbms.DBMSRowChange;
import com.taobao.tddl.dbsync.plugin.AbstractPlugin;

/**   
 */
public class SimpleApplier extends AbstractPlugin implements Applier {

	protected final Log log = LogFactory.getLog(SimpleApplier.class);
	protected AtomicLong applied = new AtomicLong();

	protected volatile DBMSEvent lastEvent;

	/**
	 * Return last applying event.
	 * 
	 * @return The last applying event.
	 */
	public DBMSEvent getLastEvent() {
		return lastEvent;
	}

	/**
	 * Return applied events number.
	 * 
	 * @return The applied events.
	 */
	public long getApplied() {
		return applied.longValue();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.taobao.tddl.dbsync.applier.Applier#apply(com.taobao.tddl.dbsync.dbms.DBMSEvent)
	 */
	public void apply(DBMSEvent event) throws ApplierException, InterruptedException {

		DBMSRowChange changeEvent = (DBMSRowChange) event;

		int rowCount = changeEvent.getRowSize();

		if (changeEvent.getAction() != DBMSAction.UPDATE) {
			return;
		}

		int colCount = changeEvent.getColumnSize();

		for (int i = 1; i < rowCount + 1; i++) {
			for (int j = 1; j < colCount; j++) {
				System.out.println(changeEvent.getRowValue(i, j));
			}
		}
	}

}
